package com.mach.platform.service;

import com.google.common.collect.Lists;
import com.mach.platform.domain.CoilingQualityWetProducts;
import com.mach.platform.domain.UnderclothAutonomousInspectionRecordTable;
import com.mach.platform.repository.CoilingQualityWetProductsRepo;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class CoilingQualityWetProductsServiceImpl  implements BaseService<CoilingQualityWetProducts>{

    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private CoilingQualityWetProductsRepo coilingQualityWetProductsRepo;

    @Override
    public CoilingQualityWetProducts save(CoilingQualityWetProducts coilingQualityWetProducts) {
        return null;
    }

    @Override
    public List<CoilingQualityWetProducts> insertObjInBatch(List<CoilingQualityWetProducts> list) {
        return null;
    }

    @Override
    public CoilingQualityWetProducts update(CoilingQualityWetProducts coilingQualityWetProducts) {
        return null;
    }

    @Override
    public int delete(Long[] ids) {
        return coilingQualityWetProductsRepo.deleteByIdIn(ids);
    }

    @Override
    public Page<CoilingQualityWetProducts> list(Pageable pageable, Map params) {
        if (params.size() > 0){
            Object startTime = params.get("startTime");
            Object endTime = params.get("endTime");
            if (startTime != null && startTime != "" && endTime != null && endTime != "") {
                LocalDateTime ldtStartTime = LocalDateTime.parse(startTime.toString(), df);
                LocalDateTime ldtEndTime = LocalDateTime.parse(endTime.toString(), df);
                Page<CoilingQualityWetProducts> result = coilingQualityWetProductsRepo.getListByTime(ldtStartTime, ldtEndTime, pageable);
                return result;
            }
        }
        return null;
    }

    @Override
    public CoilingQualityWetProducts info(Long id) {
        return null;
    }

    public void add(List<CoilingQualityWetProducts> coilingQualityWetProducts) {
        if (coilingQualityWetProducts != null && coilingQualityWetProducts.size() > 0){
            List<CoilingQualityWetProducts> newList = Lists.newArrayList();

            coilingQualityWetProducts.forEach(coilingQualityWetProduct -> {
                if (StringUtils.isNotBlank(coilingQualityWetProduct.getCustomNumber())){
                    if (coilingQualityWetProduct.getCoilingQualityWetProductsTime() == null){
                        coilingQualityWetProduct.setCoilingQualityWetProductsTime(LocalDateTime.now());
                    }
                    newList.add(coilingQualityWetProduct);
                }
            });
            //补全课长、值班主管、领班、检查员、班别
            String sectionChief = null;
            String shiftSupervisor = null;
            String foreman = null;
            String classBan = null;
            String procurator = null;
            for (CoilingQualityWetProducts coilingQualityWetProduct : coilingQualityWetProducts){
                if (StringUtils.isNotBlank(coilingQualityWetProduct.getSectionChief())){
                    sectionChief = coilingQualityWetProduct.getSectionChief();
                }
                if (StringUtils.isNotBlank(coilingQualityWetProduct.getShiftSupervisor())){
                    shiftSupervisor = coilingQualityWetProduct.getShiftSupervisor();
                }
                if (StringUtils.isNotBlank(coilingQualityWetProduct.getForeman())){
                    foreman = coilingQualityWetProduct.getForeman();
                }
                if (StringUtils.isNotBlank(coilingQualityWetProduct.getClassBan())){
                    classBan = coilingQualityWetProduct.getClassBan();
                }
                if (StringUtils.isNotBlank(coilingQualityWetProduct.getProcurator())){
                    procurator = coilingQualityWetProduct.getProcurator();
                }
            }
            for (CoilingQualityWetProducts coilingQualityWetProduct : coilingQualityWetProducts){
                coilingQualityWetProduct.setSectionChief(sectionChief);
                coilingQualityWetProduct.setShiftSupervisor(shiftSupervisor);
                coilingQualityWetProduct.setForeman(foreman);
                coilingQualityWetProduct.setClassBan(classBan);
                coilingQualityWetProduct.setProcurator(procurator);
            }

            coilingQualityWetProductsRepo.saveAll(coilingQualityWetProducts);
        }
    }

    public void update(List<CoilingQualityWetProducts> coilingQualityWetProducts) {
        this.add(coilingQualityWetProducts);
    }

    public List<CoilingQualityWetProducts> getProductionStateByTime(String time, String classBan) {
        if (time != null && !time.equals("") && classBan != null && !classBan.equals("")){
            List<CoilingQualityWetProducts> result = coilingQualityWetProductsRepo.getProductionStateByTime(time, classBan);
            return result;
        }
        return null;
    }
}
